<template>
  <div class="state" v-if="this.butlerList.length">
    <el-table v-if="som"
        :data="this.butlerList.filter(data => !search || data.userName.toLowerCase().includes(search.toLowerCase())).slice((currentPage-1)*pagesize,currentPage*pagesize)"
        style="width: 100%">
        <el-table-column align="center" prop="userName" label="用户名"></el-table-column>      
        <el-table-column align="center" prop="mobileNo" label="手机号"></el-table-column>
        <el-table-column align="center" prop="address" label="管家地址"></el-table-column>
        <el-table-column align="center" prop="startTime" label="开始时间">
          <template slot-scope="scope">
            <i class="el-icon-time" v-if="scope.row.startTime"></i>
            <span style="margin-left: 10px">{{ scope.row.startTime }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="endTime" label="结束时间">
          <template slot-scope="scope">
            <i class="el-icon-time" v-if="scope.row.endTime"></i>
            <span style="margin-left: 10px">{{ scope.row.endTime }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="price" label="价格（元）"></el-table-column>
        <el-table-column align="center" prop="isManage" label="管家状态"
          :filters="[{ text: '普通用户', value: '普通用户' }, { text: '管家会员', value: '管家会员' }, { text: '会员过期', value: '会员过期' }]"
          :filter-method="filterTag"
          filter-placement="bottom-end">
            <template slot-scope="scope">
                <el-tag 
                    :type="isManageInfo(scope.row) === '普通用户' ? 'primary' :(isManageInfo(scope.row) === '会员过期' ? 'info' : 'success')"
                    disable-transitions>{{isManageInfo(scope.row)}}</el-tag>
            </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot="header" slot-scope="scope">
          <el-input
            v-model="search"
            size="mini"
            :key="scope.row"
            placeholder="输入姓名 搜索"/>
        </template>
          <template slot-scope="scope">
              <el-button
              size="mini"
              @click="editInfo(scope.$index, scope.row)">编辑</el-button>
              <el-button
              size="mini"
              type="warning"
              @click="handleFind(scope.$index, scope.row)">查看</el-button>
          </template>
        </el-table-column>
    </el-table>
    <el-pagination v-if="som"
      class="fenye"
        align="center"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pagesize"
        layout="total, prev, pager, next"
        :total="totalList">
      >
    </el-pagination>
    <router-view />
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'
export default {
  created(){
    this.getButlerList();
  },
  data() {
    return {

      som : true,
      search: '',
      currentPage: 1,
      pagesize: 15,
    }
  },
  computed:{
    ...mapState({
        butlerList:state => state.butlerModule.butlerList
    }),
    totalList(){
      let length = this.butlerList.length;
      return length;
    },
    isManageInfo(){
        return (item) => {
          if(item.isManage == 0) {
            return '普通用户'
          }else if( item.isManage == 1){
            return '管家会员'
          }else if( item.isManage == 2){
            return '会员过期'
          }else {
            return '错误'
          }
        }
    }
  },
  methods:{
    ...mapActions({
      getButlerList:'butlerModule/getButlerList'
    }),
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
    },
    handleFind (index, row){
      this.$router.push({path:'/butler/find',query:{manageId:row.manageId}})
    },
    editInfo(index, row) {
      this.$router.push({path:'/butler/edit',query:row})
    },
    filterTag (value, row) {
      return this.isManageInfo(row) === value;
    }
  },      
  // 路由守卫判断 是否进入详情页,隐藏添加客服按钮
  beforeRouteUpdate(to, from, next){
      if((to.path !== '/butler')&&(from.path === '/butler')){
          this.som = false;
      }else{
          this.som = true;
      }
      next();
  },
  //不是详情页刷新页面，隐藏添加客服按钮
  mounted(){
      if(this.$route.path !== '/butler'){
          this.som = false;
      }
  }
}
</script>

<style lang="scss" scoped>
.state{
  width: 100%;
  background: #fff;
  padding: 10px;
  box-sizing: border-box;
  text-align: center;
}

</style>

